#include "ProcTable.h"
#include <string>
using namespace std;

ProcTable::ProcTable(){
	procIndex = map<string, ProcTableEntry>();
	revIndex = map<int, ProcTableEntry>();
	indexCount = 0;
}

int ProcTable::addProc(string procName){
	int index;

	map<string, ProcTableEntry>::iterator entryIt;
	if((entryIt = procIndex.find(procName)) != procIndex.end()){
		index = entryIt->second.getEntryNo();
	}
	else{
		index = indexCount;
		ProcTableEntry entry(procName, indexCount++);
		procIndex.insert(make_pair(procName,entry));
		revIndex.insert(make_pair(index,entry));
	}

	return index;
}

bool ProcTable::addProcNodeID(int procIndex, int nodeID){
	map<int, ProcTableEntry>::iterator entryIt = revIndex.find(procIndex);
	if(entryIt == revIndex.end()) return false;
	 return entryIt->second.addNodeID(nodeID);
}

int ProcTable::getIndex(string ProcName){
	map<string, ProcTableEntry >::iterator entryIt = procIndex.find(ProcName);
	if(entryIt == procIndex.end()) return -1;
	return entryIt->second.getEntryNo();
}

string ProcTable::getProcName(int procIndex){
	map<int, ProcTableEntry>::iterator entryIt = revIndex.find(procIndex);
	if(entryIt == revIndex.end()) return "";
	return entryIt->second.getName();
}

vector<int> ProcTable::getNodeIDs(int procIndex){
	map<int, ProcTableEntry>::iterator entryIt = revIndex.find(procIndex);
	if(entryIt == revIndex.end()) return vector<int>();
	return entryIt->second.getNodeIDs();
}

int ProcTable::size(){
	return indexCount;
}
